package ochart.renders.vector.elements.labels
{
	import flash.events.Event;
	import flash.text.TextField;
	
	import ochart.elements.labels.LabelBase;
	import ochart.renders.vector.VectorRenderer;
	
	public class LabelBaseRenderer extends VectorRenderer
	{
		public function LabelBaseRenderer()
		{
			super();
		}
		
		override public function render():Boolean
		{
			if(!super.render())
			{
				return false;
			}
			
			var labelBase:LabelBase = target as LabelBase;
			if(labelBase.content && labelBase.contains(labelBase.content))
			{
				labelBase.removeChild(labelBase.content);
			}
			var textField:TextField = new TextField();
			textField.x = labelBase.paddingLeft;
			textField.y = labelBase.paddingTop;
			textField.multiline = labelBase.multiline;
			var htmlText:String = "<font size='" + labelBase.size + "'>" + labelBase.text + "</font>";
			if(labelBase.bold)
			{
				htmlText = "<b>" + htmlText + "</b>";
			}
			if(labelBase.italic)
			{
				htmlText = "<i>" + htmlText + "</i>";
			}
			if(labelBase.underline)
			{
				htmlText = "<u>" + htmlText + "</u>";
			}
			textField.htmlText = htmlText;
			textField.textColor = labelBase.color;
			if(labelBase.autoSize)
			{
				textField.height = labelBase.paddingTop + textField.textHeight + 4 + labelBase.paddingBottom;
				textField.width = labelBase.paddingLeft + textField.textWidth + 4 + labelBase.paddingRight;
				labelBase.height = textField.height;
				labelBase.width = textField.width;
				
				// 由于尺寸是随内容改变,所以此处需要重新画背景
				renderBackground();
			}
			labelBase.content = textField;
			labelBase.addChild(labelBase.content);
			
			return true;
		}
	}
}